package com.cdpowernode.pncrm.controller;

import com.cdpowernode.pncrm.exception.CrmException;
import com.cdpowernode.pncrm.javabean.User;
import com.cdpowernode.pncrm.javabean.UserVo;
import com.cdpowernode.pncrm.service.UserService;
import com.cdpowernode.pncrm.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author jy
 * @version 1.0
 * @date 2021/12/3 11:12
 */
@Controller
@RequestMapping("/settings/qx/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "settings/qx/user/login";
    }

    @RequestMapping("/toMain")
    public String toMain(){
        return "workbench/main/index";
    }

    @RequestMapping(value = "/main",method = RequestMethod.POST)
    public String login(UserVo userVo, Model model, HttpServletRequest request, HttpServletResponse response, HttpSession session){
        //获取客户端的ip地址
        String remoteAddr = request.getRemoteAddr();
        try {
            User user = userService.login(userVo, remoteAddr);
            //将登陆成功的用户存入session中
            session.setAttribute("session_login_user",user);
            //如果用户勾选了：记住我10天  则将登录的用户名与密码存入cookie 10天
            if (userVo.isRememberme()){
                addUserInfoToCookie(userVo,response);
            }else {
                removeUserInfoFromCookie(request,response);
            }
            //登录成功 跳转到业务主页面
            return "workbench/index";
        }catch (Exception e){
            if (e instanceof CrmException){
                model.addAttribute("errMsg",e.getMessage());
            }else {
                model.addAttribute("errMsg","系统异常！");
            }
            return "settings/qx/user/login";
        }
    }

    /**
     * 登出，清空cookie，销毁session
     * @param request
     * @param response
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request,HttpServletResponse response,HttpSession session){
        //将保存登录信息的cookie删除
        removeUserInfoFromCookie(request,response);
        //将保存登录用户的session的内容删除
        session.removeAttribute(Constant.SESSION_LOGIN_USER);
        return "redirect:/";
    }




    /**
     * 删除记录了用户名与密码的cookie
     * @param response
     * @param request
     */
    private void removeUserInfoFromCookie(HttpServletRequest request,HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null){
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("loginAct") || cookie.getName().equals("loginPwd")){
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
        }

    }

    /**
     * 将登录成功用户的用户名与密码存入cookie 10天
     * @param userVo
     * @param response
     */
    private void addUserInfoToCookie(UserVo userVo, HttpServletResponse response) {
        Cookie cookie1 = new Cookie("loginAct", userVo.getLoginAct());
        Cookie cookie2 = new Cookie("loginPwd", userVo.getLoginPwd());

        //设置cookie的生命周期(单位是秒)
        cookie1.setMaxAge(60 * 60 * 24 * 10);
        cookie2.setMaxAge(60 * 60 * 24 * 10);

        //将两个cookie发送给login界面
        response.addCookie(cookie1);
        response.addCookie(cookie2);
    }
}
